package com.hoperun.nocserver.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.nutz.log.Log;
import org.nutz.log.Logs;

/**
 * java 操作 command工具类
 * 
 * @author zhu_longxiang
 *
 */
public class CommandUtil {

	private final static Log LOGGER = Logs.get();

	/**
	 * 執行cmd命令（本机）
	 * 
	 * @param commandStr
	 *            cmd命令字符串
	 * @return 执行结果
	 */
	public static String exeCmd(String commandStr) {
		BufferedReader br = null;
		String result = "";
		try {
			Process p = Runtime.getRuntime().exec(commandStr);
			br = new BufferedReader(new InputStreamReader(p.getInputStream(), Charset.forName("GBK")));
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			result = sb.toString();
		} catch (Exception e) {
			LOGGER.error("执行exeCmd发生异常", e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (Exception e) {
					LOGGER.error("执行exeCmd关闭流异常", e);
				}
			}
		}
		return result;
	}

}
